package com.zy.socket.chat_0;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/12/19 18:57
 **/
public class LinkServerFrame extends JFrame{
    private JPanel contentPane;//下方面板
    private JLabel lblIP;
    private JLabel lblUserName;
    private JTextField tfIP;
    private JTextField tfUserName;
    private JButton btnLink;


    public LinkServerFrame(){
        setTitle("连接服务器");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 390, 150);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5,5,5,5));
        contentPane.setLayout(null);
        setContentPane(contentPane);

        lblIP =new JLabel("服务器IP地址:");
        lblIP.setFont(new Font("微软雅黑",Font.PLAIN,14));
        lblIP.setBounds(20,15,100,15);
        contentPane.add(lblIP);

        tfIP =new JTextField("127.0.0.1");
        tfIP.setBounds(121,13,242,21);
        contentPane.add(tfIP);
        tfIP.setColumns(10);

        lblUserName = new JLabel("用户名:");
        lblUserName.setFont(new Font("微软雅黑",Font.PLAIN,14));
        lblUserName.setBounds(60,40,60,15);
        contentPane.add(lblUserName);

        tfUserName =new JTextField();
        tfUserName.setBounds(121,42,242,21);
        contentPane.add(tfUserName);
        tfUserName.setColumns(10);

        btnLink =new JButton("连接服务器");
        btnLink.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                do_btnlink_actionPerformed(e);
            }
        });
        btnLink.setFont(new Font("微软雅黑", Font.PLAIN,14));
        btnLink.setBounds(140,80,120,23);
        contentPane.add(btnLink);//显示连接服务器窗口
    }
    public static void main(String[] args){
        LinkServerFrame linkServerFrame = new LinkServerFrame();
        linkServerFrame.setVisible(true);
    }
    protected void do_btnlink_actionPerformed(ActionEvent e) {
        if(!tfIP.getText().equals("")&&!tfUserName.getText().equals("")) {//文本框的内容不能为空
            dispose();//销毁当前窗口
            ClientFrame clientFrame=new ClientFrame(tfIP.getText().trim(), tfUserName.getText().trim());
            clientFrame.setVisible(true);//显示客户窗体
        }
        else {
            JOptionPane.showMessageDialog(null,"文本框里的内容不能为空！","警告",JOptionPane.WARNING_MESSAGE);
        }
    }
}
